// models/User.js
import mongoose from 'mongoose';

const { Schema } = mongoose;

const userSchema = new Schema(
    {
        name: {
            type: String,
            required: true,
            unique: true,
        },
        email: {
            type: String,
            required: true,
            unique: true,
        },
        password: {
            type: String,
            required: true,
        },
        avatar: {
            type: String,
            default: "/contact.png",
        },
        bio: {
            type: String,
            default: "这个人很懒，还没有填写简介~",
        },
        userType: {
            type: String,
            enum: ["user", "admin"],
            default: "user",
        },
        registerIp: String, // 新增：记录注册时的IP地址
        sessions: [
            {
                sessionId: String,
                deviceInfo: String,
                ipAddress: String,
                createdAt: { type: Date, default: Date.now }
            }
        ],
        // 新增：上次登录信息
        lastLogin: {
            ipAddress: String,
            deviceInfo: String,
            timestamp: { type: Date, default: null }
        }
    },
    {
        timestamps: true,
    }
);

export default mongoose.models.User || mongoose.model("User", userSchema);